package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.GeoRegion;
import com.tbit.uqbike.service.business.GeoRegionBoundService;
import com.tbit.uqbike.service.business.GeoRegionService;
import com.tbit.uqbike.webmanager.dao.core.GeoRegionDao;
import com.tbit.utils.GeoUtil;
import com.tbit.utils.TestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 围栏分区(GeoRegion)表服务实现类
 *
 * @author jing
 * @since 2020-11-23 16:36:00
 */
@Service("geoRegionService")
public class GeoRegionServiceImpl implements GeoRegionService {
    @Resource
    private GeoRegionDao geoRegionDao;
    @Resource
    private GeoRegionBoundService geoRegionBoundService;


    /**
     * 通过ID查询单条数据
     *
     * @param regionId 主键
     * @return 实例对象
     */
    @Override
    public GeoRegion queryById(Integer regionId) {
        return this.geoRegionDao.queryById(regionId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<GeoRegion> queryAllByLimit(int offset, int limit) {
        return this.geoRegionDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据,数据存在则修改
     *
     * @param geoRegion 实例对象
     * @return 实例对象
     */
    @Override
    public GeoRegion insert(GeoRegion geoRegion) {
        List<Point> points = GeoUtil.getPoints(geoRegion.getPoints());
        Point newPoint = GeoUtil.GetCenterPoint(points);
        geoRegion.setCenterLat(newPoint.getY());
        geoRegion.setCenterLon(newPoint.getX());
        geoRegion.setUpdateTime(new Date());
        GeoRegion oldGeo = geoRegionDao.queryById(geoRegion.getRegionId());
        List<GeoRegion> geoRegions = geoRegionDao.getReginon(geoRegion.getAccountId());
        if (null == oldGeo) {
            GeoRegion geoRegionByName = geoRegionDao.getByGeoRegionName(geoRegion.getAccountId(), geoRegion.getRegionName());
            if (geoRegionByName != null) {
                throw new BaseException("该运营区内有重复的分区名称");
            }
            /**判断添加的分区是否重叠*/
            for (GeoRegion region : geoRegions) {
                if (TestUtil.RegionOverlap(geoRegion.getPoints(), region.getPoints())) {
                    throw new BaseException(SystemCodeEnum.REGION_OVERLAP_ERROR);
                }
            }
            Integer flag = geoRegionDao.queryByName(geoRegion.getRegionName(),geoRegion.getAccountId());
            if (1 == flag) {
                throw new BaseException(SystemCodeEnum.DUPKLICATE_NAME);
            }
            this.geoRegionDao.insert(geoRegion);
            geoRegion = geoRegionDao.queryById(geoRegion.getRegionId());
        } else {
            GeoRegion geoRegionByName = geoRegionDao.getByGeoRegionName(geoRegion.getAccountId(), geoRegion.getRegionName());
            if (geoRegionByName != null && !Objects.equals(geoRegion.getRegionId(), geoRegionByName.getRegionId())) {
                throw new BaseException("该运营区内有重复的分区名称");
            }
            /**判断添加的分区是否重叠*/
            for (GeoRegion region : geoRegions) {
                /**判断修改的分区是否和原本的一样*/
                if (!geoRegion.getRegionId().equals(region.getRegionId())) {
                    if (TestUtil.RegionOverlap(geoRegion.getPoints(), region.getPoints())) {
                        throw new BaseException(SystemCodeEnum.REGION_OVERLAP_ERROR);
                    }
                }
            }
            geoRegion = update(geoRegion);
        }

        return geoRegion;
    }

    /**
     * 修改数据
     *
     * @param geoRegion 实例对象
     * @return 实例对象
     */
    @Override
    public GeoRegion update(GeoRegion geoRegion) {
        this.geoRegionDao.update(geoRegion);
        return this.queryById(geoRegion.getRegionId());
    }

    /**
     * 通过主键删除数据
     *
     * @param regionId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer regionId) {
        /**删除分区绑定的运维人员*/
        geoRegionBoundService.deleteById(regionId, null);
        boolean b = this.geoRegionDao.deleteById(regionId) > 0;
        return b;
    }

    @Override
    public List<GeoRegion> queryByGeoRegion(GeoRegion geoRegion) {
        PageHelper.startPage(geoRegion.getPage().getPageNO(), geoRegion.getPage().getRowCount());
        return geoRegionDao.selectByGeoRegion(geoRegion);

    }

    @Override
    public GeoRegion getByGeoRegionName(Integer accountId, String regionName) {
        return geoRegionDao.getByGeoRegionName(accountId, regionName);
    }

    @Override
    public List<Integer> getByAccountUserId(Integer accountUserId) {
        return geoRegionDao.getByAccountUserId(accountUserId);
    }
}